﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WebNetCorePhoneErp.Model;

namespace WebNetCorePhoneErp.Controllers
{
    /// <summary>
    /// 控制器基类，提供基础数据
    /// </summary>
    /// <remarks>
    /// 作者：王浩力
    /// 创建时间：2021-6-11 14:08:05 
    /// </remarks>
    public class BaseController : Controller
    {
        /// <summary>
        /// 当前登录用户
        /// </summary>
        protected UserView CurrentUser
        {
            get
            {
                string sessionJson = HttpContext.Session.GetString("user");
                UserView userView = null;
                try
                {
                    userView = System.Text.Json.JsonSerializer.Deserialize<UserView>(sessionJson);
                }
                catch (Exception)
                {
                    userView = null;
                }
                return userView;
            }
        }

        /// <summary>
        /// 获取客户端ip
        /// </summary>
        protected string GetCustomerIp
        {
            get
            {
                var ip = this.Request.Headers["X-Forwarded-For"].FirstOrDefault();
                if (string.IsNullOrEmpty(ip))
                {
                    ip = this.Request.HttpContext.Connection.RemoteIpAddress.MapToIPv4().ToString();
                }
                return ip;
            }
        }

    }
}
